<template>
    <div class="group">
        <h3>{{title}}</h3>
        <h4>param: </h4>
        <p v-for="(value,key) in param.value">{{key}}:{{value.name}}<input type="text" v-model="param.value[key]"></p>
        <h4>url:'{{param.url}}'</h4>
        <button class="btn" v-on:click="post()">获取{{Ftitle}}数据</button>
        <p v-for="(value,key) in param.beizhu">{{value.line}}</p>
    </div>
</template>

<script>
	import md5 from 'js-md5';
	export default{
		data(){
			return {
				
			}
		},
		components:{

		},
		computed:{
			Ftitle(){
				return this.title.slice(this.title.indexOf('、')+1)
			}
		},
		props:['title','param'],
		methods:{
			post( ){
                const userAgentDefalut = '51jili';
                const appid = '06wygzvDdr062rNwIXTC';
                const appkey = 'vxCdATZ76WeqjhF3ZNHu';
                const appver = '2.6.0';
                const apptype = 'ios';

                if(this.param.value){
                    // 深拷贝一个副本,若不这样做，会把data也污染
                    var valuesObj = JSON.parse(JSON.stringify(this.param.value));
                    console.log('深拷贝')
                    console.log(valuesObj);
                    arr = [],
                    str = '',
                    hash;
                }else{
                    var valuesObj = {},
                    arr = [],
                    str = '',
                    hash;
                }

                valuesObj.time = Date.parse(new Date());
                valuesObj.appver = appver;
                valuesObj.apptype = apptype;

                for(var item in valuesObj){
                  // php接受post的值若为空，则不接受这个键名，并且后台序列化加密的键名里面不出现
                  if(valuesObj[item]!=''){
                    var temp = item+':'+valuesObj[item];
                    arr.push(temp);
                  }
                }
                arr.sort();
                console.log(String(arr))
                for(var i of arr){
                  var index = i.indexOf(':');
                  var temp = i.slice(0,index)+'='+i.slice(index+1);
                  str += temp+'&';
                }
                str += 'appid='+appid+'&appkey='+appkey;
                console.log(str)

                hash = md5(str);
                valuesObj.sign = hash.toLocaleUpperCase();

                console.log(valuesObj)
				$.ajax({
                    url:this.param.url,
                    type:'post',
                    dataType:'json',
                    data:valuesObj,
                    success:function(data){
                        console.log(data)
                        console.log(JSON.stringify(data))
                    }
                })

        	},
		},

	}
</script>

<style>
	 .btn{
        background:#e4393c;
        padding:5px;
        color:#fff;
    }
</style>